﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnWeb.DTO;
using DoAnWeb.BUS;
using DoAnWeb.DAO;

namespace DoAnWeb
{
    public partial class Information : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!CurrentContext.IsLogged)
                Response.Redirect("~/Login.aspx");
                if (!IsPostBack)
                {
                    lbtnThongTinTaiKhoan.Visible = true;
                    lbtnThayDoiMatKhau.Visible = true;

                    pnMember.Visible = false;
                    pnDoanhNghiep.Visible = false;
                    pnAdmin.Visible = false;

                    TaiKhoanDAO DAO = new TaiKhoanDAO();
                    ChangePanel("ThongTinTaiKhoan");
                    LoadThongTinTaiKhoan();
                    if (DAO.GetKind(CurrentContext.UserName) == "LTK1")
                        pnAdmin.Visible = true;
                    else if (DAO.GetKind(CurrentContext.UserName) == "LTK3")
                        pnDoanhNghiep.Visible = true;
                    else
                        pnMember.Visible = true;
                }
            }
        }

        protected void lbtnThongTinTaiKhoan_Click(object sender, EventArgs e)
        {
            ChangePanel("ThongTinTaiKhoan");
        }

        protected void lbtnThayDoiMatKhau_Click(object sender, EventArgs e)
        {
            ChangePanel("ThayDoiMatKhau");
        }

        protected void lbtnCacDealDaMua_Click(object sender, EventArgs e)
        {
            ChangePanel("CacDealDaMua");
        }

        protected void lbtnLichSuaMuaHang_Click(object sender, EventArgs e)
        {

        }

        protected void lbtnDanhSachDatGiao_Click(object sender, EventArgs e)
        {
            ChangePanel("DanhSachDatGiao");
        }

        protected void lbtnQuanLyKhuyenMai_Click(object sender, EventArgs e)
        {

        }

        protected void btnLuuThayDoimatKhau_Click(object sender, EventArgs e)
        {
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            TaiKhoanDTO DTO = new TaiKhoanDTO();
            lbMatKhauHienTai.Visible = false;
            lbThayDoiMatKhauThanhCong.Visible = false;
            if (BUS.CheckExist(CurrentContext.UserName, txtMatKhauHienTai.Text))
            {
                BUS.Update_PassWord(CurrentContext.UserName, txtMatKhauMoi.Text);
                lbThayDoiMatKhauThanhCong.Visible = true;
            }
            else
            {
                txtMatKhauHienTai.Focus();
                lbMatKhauHienTai.Visible = true;
            }
        }

        protected void btnLuuThongTinTaiKhoan_Click(object sender, EventArgs e)
        {
            lbThongTinTaiKhoan.Visible = true;

            TaiKhoanBUS BUS = new TaiKhoanBUS();
            TaiKhoanDTO DTO = new TaiKhoanDTO();
            TaiKhoanDTO tk = BUS.GetFromId(CurrentContext.UserName);

            DTO.DiaChi = txtDiaChi.Text;
            DTO.DienThoai = txtDienThoai.Text;
            DTO.Email = txtEmail.Text;
            DTO.GioiTinh = (rdNam.Checked) ? "Nam" : "Nữ";
            //DTO.HinhAnh = imgHinhDaiDien.ImageUrl;
            DTO.HoTen = txtHoTen.Text;
            //DTO.MaLoaiTaiKhoan = tk.MaLoaiTaiKhoan;
            //DTO.MatKhau = tk.MatKhau;
            //DTO.NgayDangKy = Convert.ToDateTime(lbNgayDangKy.Text);
            DTO.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
            DTO.TenDoanhNghiep = txtTenDoanhNghiep.Text;
            DTO.TenTaiKhoan = CurrentContext.UserName;

            if (BUS.Update(DTO))
                lbThongTinTaiKhoan.Text = "Thay đổi thành công.";
            else
                lbThongTinTaiKhoan.Text = "Thay đổi thất bại.";

        }

        void LoadThongTinTaiKhoan()
        {
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            TaiKhoanDTO DTO = BUS.GetFromId(CurrentContext.UserName);
            imgHinhDaiDien.ImageUrl = "/images/User/" + DTO.HinhAnh;

            lbTenTaiKhoan.Text = DTO.TenTaiKhoan;

            if (BUS.GetKind(CurrentContext.UserName) == "LTK3")
            {
                txtTenDoanhNghiep.Text = DTO.TenDoanhNghiep;
                pnHoTenMember.Visible = false;
            }
            else
            {
                txtHoTen.Text = DTO.HoTen;
                pnTenDoanhNghiep.Visible = false;
            }
            txtNgaySinh.Text = DTO.NgaySinh.ToShortDateString();
            rdNam.Checked = (DTO.GioiTinh == "Nam") ? true : false;
            rdNu.Checked = !rdNam.Checked;
            txtDiaChi.Text = DTO.DiaChi;
            txtDienThoai.Text = DTO.DienThoai;
            txtEmail.Text = DTO.Email;
            lbNgayDangKy.Text = DTO.NgayDangKy.ToShortDateString();
            lbLoaiTaiKhoan.Text = DTO.TenLoaiTaiKhoan;
        }

        protected void ChangePanel(string _pnName)
        {
            pnThongTinTaiKhoan.Visible = false;
            pnThayDoiMatKhau.Visible = false;
            pnCacDealDaMua.Visible = false;
            lbThongBao.Visible = false;
            lbTieuDe.Visible = true;
            upnDanhSachDatGiao.Visible = false;
            dlCacDealDaMua.DataSource = null;
            gvDanhSachDatHang.DataSource = null;

            switch (_pnName)
            {
                case "ThongTinTaiKhoan":
                    lbThongTinTaiKhoan.Visible = false;
                    pnThongTinTaiKhoan.Visible = true;
                    LoadThongTinTaiKhoan();
                    lbTieuDe.Text = "Thông tin tài khoản";
                    break;
                case "ThayDoiMatKhau":
                    pnThayDoiMatKhau.Visible = true;
                    lbTieuDe.Text = "Thay đổi mật khẩu";
                    break;
                case "CacDealDaMua":
                    pnCacDealDaMua.Visible = true;
                    dlCacDealDaMua.DataSource = new TinKhuyenMaiBUS().GetList_TenTaiKhoan(CurrentContext.UserName);
                    dlCacDealDaMua.DataBind();

                    if (dlCacDealDaMua.Items.Count <= 0)
                        lbThongBao.Visible = true;

                    lbTieuDe.Text = "Tình trạng các deal đã mua";
                    break;
                case "DanhSachDatGiao":
                    upnDanhSachDatGiao.Visible = true;
                    gvDanhSachDatHang.DataSource = new CT_DonHangBUS().GetList_DoanhNghiep(CurrentContext.UserName);
                    gvDanhSachDatHang.DataBind();
                    lbTieuDe.Text = "Danh sách được đặt giao";
                    break;
                case "":
                    break;
                default:
                    break;
                    
            }
        }

        protected bool TinhTrang(int _MaTinKhuyenMai)
        {
            int SoLuongConLai = new TinKhuyenMaiBUS().CountNumberRemain(_MaTinKhuyenMai);
            if (SoLuongConLai <= 0)
                return false;
            else
                return true;
        }

        protected void gvDanhSachDatHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "btnCapNhat")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());
                int MaCTDonHang = Convert.ToInt32((gvDanhSachDatHang.Rows[index].Cells[0].Controls[1] as Label).Text);
                string DaGiao = (gvDanhSachDatHang.Rows[index].Cells[7].Controls[1] as Label).Text;
                if (DaGiao == "Đã giao")
                    new CT_DonHangBUS().UpdateChuaGiao(MaCTDonHang);
                else
                    new CT_DonHangBUS().UpdateDaGiao(MaCTDonHang);
                ChangePanel("DanhSachDatGiao");
            }
        }

        protected void gvDanhSachDatHang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected string SoLuotThamGia(int _MaTinKhuyenMai)
        {
            int SoLuot = new TinKhuyenMaiBUS().CountNumberSelled(_MaTinKhuyenMai);
            if (SoLuot <= 0)
                return "Hãy là người đầu tiên.";
            else
                return SoLuot.ToString() + " lượt.";
        }
        //---------------------------------------------------------

    }
}